package com.lib.framework.manager

import android.app.Activity
import android.app.Application
import android.os.Bundle

/**
 * 前后台切换监听 管理
 *
 * @author keanbin
 */
object ForegroundBackgroundManager {

    private const val TAG = "com.weipeng.framework.manager.ForegroundBackgroundManager"

    /**
     * 页面 start 计数，stop 时 减一
     */
    private var mStartActivityCount = 0

    /**
     * 是否在前台
     */
    private var mIsForeground = true

    /**
     * 前后台切换监听 列表
     */
    private val mForegroundBackgroundListener = mutableListOf<ForegroundBackgroundListener>()

    fun init(application: Application) {
        application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityPaused(activity: Activity?) {

            }

            override fun onActivityResumed(activity: Activity?) {

            }

            override fun onActivityStarted(activity: Activity?) {
                mStartActivityCount++

                LogManager.i(TAG, "mStartActivityCount = $mStartActivityCount")
                //如果 mStartActivityCount == 1，说明是从后台到前台
                if (mStartActivityCount == 1) {
                    LogManager.i(TAG, "后台 to 前台")
                    notifyAll(true)
                }
            }

            override fun onActivityDestroyed(activity: Activity?) {

            }

            override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {

            }

            override fun onActivityStopped(activity: Activity?) {
                mStartActivityCount--

                LogManager.i(TAG, "mStartActivityCount = $mStartActivityCount")
                //如果 mStartActivityCount == 0，说明是前台到后台
                if (mStartActivityCount == 0) {
                    LogManager.i(TAG, "前台 to 后台")
                    notifyAll(false)
                }
            }

            override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {

            }
        })
    }

    /**
     * 注册 前后台切换监听器
     */
    fun registerListener(l: ForegroundBackgroundListener) {
        mForegroundBackgroundListener.add(l)
    }

    /**
     * 注销 前后台切换监听器
     */
    fun unregisterListener(l: ForegroundBackgroundListener) {
        mForegroundBackgroundListener.remove(l)
    }

    /**
     * 通知所有的监听器
     */
    private fun notifyAll(isSwitchToForeground: Boolean) {
        mIsForeground = isSwitchToForeground
        for (l in mForegroundBackgroundListener) {
            if (isSwitchToForeground) {
                l.onSwitchToForeground()
            } else {
                l.onSwitchToBackground()
            }
        }
    }

    /**
     * 是否在前台
     */
    fun isForeground(): Boolean {
        return mIsForeground
    }

    /**
     * 前后台切换监听器
     */
    interface ForegroundBackgroundListener {

        /**
         * 切换到前台
         */
        fun onSwitchToForeground()

        /**
         * 切换到后台
         */
        fun onSwitchToBackground()
    }
}